package com.dw.article.job;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import com.dw.article.service.ArticleUserService;
import com.dw.model.article.entitys.ArticleUser;
import com.dw.model.article.vos.article.QueryUserArticleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

@Slf4j
@Component
public class GiveUpTask {

    @Resource
    private ArticleUserService articleUserService;

    @Resource
    private RedisTemplate redisTemplate;

    private final String ARTICLE_LIKED_KEY = "article.liked";

    private final String ARTICLE_STAR_KEY = "article.star";

    private final String ARTICLE_SHARE_KEY = "article.shared";

    /**
     * 同步点赞数据
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void syncLiked() {
        log.info("定时处理评论区点赞数据", LocalDateTime.now());
        // 查询缓存中的所有点赞
        Map<Object, Object> likeEntries = redisTemplate.opsForHash().entries(ARTICLE_LIKED_KEY);
        if(MapUtil.isEmpty(likeEntries)){
            return;
        }
        LinkedList<ArticleUser> articleUsers = new LinkedList<>();
        // 将缓存中的点赞数据同步至数据库
        likeEntries.forEach((key, value) -> {
            ArticleUser articleUser = new ArticleUser();
            String[] keyArr = key.toString().split(":");
            Long articleId = Long.valueOf(keyArr[0]);
            Long userId = Long.valueOf(keyArr[1]);
            articleUser.setArticleId(articleId);
            articleUser.setUserId(userId);
            articleUser.setIsLiked((Integer) value);
            // 生成雪花算法id
            long leiId = IdUtil.getSnowflake().nextId();
            articleUser.setLeiId(leiId);
            articleUsers.add(articleUser);
        });
        articleUserService.saveOrUpdateBatch(articleUsers);
    };

    /**
     * 同步收藏数据
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void syncStar() {
        log.info("定时处理评论区收藏数据", LocalDateTime.now());
        // 查询缓存中的所有收藏
        Map<Object, Object> starEntries = redisTemplate.opsForHash().entries(ARTICLE_STAR_KEY);
        if(MapUtil.isEmpty(starEntries)){
            return;
        }
        LinkedList<ArticleUser> articleUsers = new LinkedList<>();
        // 将缓存中的收藏数据同步至数据库
        starEntries.forEach((key,value) -> {
            ArticleUser articleUser = new ArticleUser();
            String[] keyArr = key.toString().split(":");
            Long articleId = Long.valueOf(keyArr[0]);
            Long userId = Long.valueOf(keyArr[1]);
            articleUser.setArticleId(articleId);
            articleUser.setUserId(userId);
            articleUser.setIsStar((Integer) value);
            // 生成雪花算法id
            long leiId = IdUtil.getSnowflake().nextId();
            articleUser.setLeiId(leiId);
            articleUsers.add(articleUser);
        });
        articleUserService.saveOrUpdateBatch(articleUsers);
    }

    /**
     * 同步转发数据
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void syncShared() {
        log.info("定时处理评论区转发数据", LocalDateTime.now());
        Map<Object, Object> sharedEntries = redisTemplate.opsForHash().entries(ARTICLE_SHARE_KEY);
        if(MapUtil.isEmpty(sharedEntries)){
            return;
        }
        LinkedList<ArticleUser> articleUsers = new LinkedList<>();
        sharedEntries.forEach((key,value) -> {
            ArticleUser articleUser = new ArticleUser();
            String[] keyArr = key.toString().split(":");
            Long articleId = Long.valueOf(keyArr[0]);
            Long userId = Long.valueOf(keyArr[1]);
            articleUser.setArticleId(articleId);
            articleUser.setUserId(userId);
            articleUser.setIsShared((Integer) value);
            // 生成雪花算法id
            long leiId = IdUtil.getSnowflake().nextId();
            articleUser.setLeiId(leiId);
            articleUsers.add(articleUser);
        });
        articleUserService.saveOrUpdateBatch(articleUsers);
    }

}
